package java行为型设计模式.命令模式;

import java.util.ArrayList;
import java.util.List;


/**
 * 命令模式：将一个请求封装为一个对象，从而使你可用不同的请求对客户进行参数化，
 * 对请求排队或记录请求日志。以及支持可撤销的操作。
 *
 * 说白了，就是将一系列的请求命令封装起来，不直接调用真正执行者的方法，这样比较好扩展。
 */
public class Main {
    public static void main(String[] args) {
        MouseCursor cursor = new MouseCursor();
        MoveCursor moveCursor = new MoveCursor(cursor);
        ResetCursor resetCursor = new ResetCursor(cursor);

        MouseCommands commands = new MouseCommands();
        commands.takeOrder(moveCursor);
        commands.takeOrder(resetCursor);

        commands.placeOrders();
    }
}
interface Command{
    void execute();
}
class MouseCursor{
    private int x = 10;
    private int y = 10;
    public void move(){
        System.out.println("Old Position:" + x + ":" +y);
        x++;
        y++;
        System.out.println("New Position:" + x + ":" + y);
    }
    public void rest(){
        System.out.println("rest");
        x = 10;
        y = 10;
    }
}
class MoveCursor implements Command{
    private MouseCursor abcStock;

    public MoveCursor(MouseCursor abcStock) {
        this.abcStock = abcStock;
    }

    @Override
    public void execute() {
        abcStock.move();
    }

}
class ResetCursor implements Command{
    private MouseCursor avcStock;

    public ResetCursor(MouseCursor avcStock) {
        this.avcStock = avcStock;
    }

    @Override
    public void execute() {
        avcStock.rest();
    }

}
class MouseCommands{
    private List<Command> orderList = new ArrayList<>();
    public void takeOrder(Command order){
        orderList.add(order);
    }
    public void placeOrders(){
        for (Command order : orderList) {
            order.execute();
        }
        orderList.clear();
    }
}


